var fs = require('fs');

// 打开一个流:highWaterMark 设置一次性读取的字节长度
let os = require("os");
let startMem = os.freemem();//系统的空闲内存
var readerStream = fs.createReadStream('sample.txt', {highWaterMark:3});
readerStream.setEncoding('UTF8');
// 处理流事件 --> data, end, and error
readerStream.on('open',function(){
   console.log('打开了文件');
})
// 一行行的读取文件
readerStream.on('data', function(chunk) {
   console.log(chunk+'===================');
   data += chunk;
});
readerStream.on('end',function(){
   //end时读取完成
   let endMem = os.freemem();
   console.log((endMem-startMem)/1024/1024);//内存大小的基本单位时字节 /1024(转位kb)/1024(转位mb)
   console.log(data);
});

readerStream.on("close",function(){
  console.log('结束了');
});
